package com.eqxiu.ieditor.manager.controller;

import com.eqxiu.bigdata.common.api.CommonApi;
import com.eqxiu.bigdata.common.dto.WapSceneDataDTO;
import com.eqxiu.bigdata.common.exception.WapSceneDataException;
import com.eqxiu.ieditor.manager.utils.RequestUtils;
import com.google.common.collect.Maps;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.Period;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/m/ie/scene")
public class SceneStatController {
    @Autowired
    private CommonApi bigdataApi;

    private final DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");

    private static final int PV_MAX = 100000;

    private final Logger logger = LoggerFactory.getLogger(SceneStatController.class);

    @RequestMapping("/{page}")
    public String index(@PathVariable String page) {
        return "scene/" + page;
    }

    /**
     * @throws WapSceneDataException
     * @description 根据条件分页查找订单
     * @author ranger
     * @date 2017年5月5日
     */
    @PostMapping("/overviewList")
    @ResponseBody
    public Map<String, Object> findSceneOverview(HttpServletRequest request, HttpServletResponse response)
            throws WapSceneDataException {
        // 按条件过滤
        Map<String, String> params = RequestUtils.getRequestParamMap(request);
        LocalDateTime now = LocalDateTime.now();
        String startTime = params.getOrDefault("startTime", now.minusWeeks(1).format(formatter));
        String endTime = params.getOrDefault("endTime", now.format(formatter));
        int pvEffective = Integer.parseInt(params.getOrDefault("pvEffective", "10"));
        LocalDate startDate = LocalDate.parse(startTime);
        LocalDate endDate = LocalDate.parse(endTime);
        java.text.DecimalFormat df = new java.text.DecimalFormat("#.##");
        int numOfDays = Period.between(startDate, endDate).getDays() + 1;
        List<Map<String, String>> dtos = new ArrayList<>();
        for (int i = 0; i < numOfDays; i++) {
            Map<String, String> result = new HashMap<>();
            String date = startDate.plusDays(i).format(formatter);
            long totalScene = bigdataApi.queryWapSceneDataCount(date + " 00:00:00", date + " 23:59:59", 0, PV_MAX);
            long effectiveScene = bigdataApi.queryWapSceneDataCount(date + " 00:00:00", date + " 23:59:59", pvEffective, PV_MAX);
            result.put("date", date);
            result.put("total", totalScene + "");
            result.put("effective", effectiveScene + "");
            result.put("effectiveRatio", totalScene == 0L ? "0%" : df.format((effectiveScene + 0.0) / totalScene * 100) + "%");
            dtos.add(result);
        }

        Map<String, Object> map = Maps.newHashMap();
        map.put("total", numOfDays);
        map.put("rows", dtos);
        return map;
    }

    /**
     * @throws WapSceneDataException
     * @description 根据条件分页查找订单
     * @author ranger
     * @date 2017年5月5日
     */
    @PostMapping("/list")
    @ResponseBody
    public Map<String, Object> findSceneList(HttpServletRequest request, HttpServletResponse response)
            throws WapSceneDataException {
        // 按条件过滤
        Map<String, String> params = RequestUtils.getRequestParamMap(request);
        Integer pageNumber = Integer.parseInt(params.get("pageNumber"));
        Integer pageSize = Integer.parseInt(params.get("pageSize"));
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd hh:mm:ss");
        LocalDateTime now = LocalDateTime.now();

        String startTime = params.getOrDefault("startTime", now.minusWeeks(1).format(formatter));
        String endTime = params.getOrDefault("endTime", now.format(formatter));

        String minPvStr = params.getOrDefault("minPv", "0");
        String maxPvStr = params.getOrDefault("maxPv", PV_MAX + "");
        int minPv = Integer.parseInt(minPvStr);
        int maxPv = Integer.parseInt(maxPvStr);

        boolean pvOrder = Boolean.FALSE;
        List<WapSceneDataDTO> dtos = bigdataApi.queryWapSceneData(startTime, endTime, minPv, maxPv, pageNumber, pageSize, pvOrder);
        Map<String, Object> map = Maps.newHashMap();
        map.put("total", bigdataApi.queryWapSceneDataCount(startTime, endTime, minPv, maxPv));
        map.put("rows", dtos);
        return map;
    }

}
